﻿
// FirstimaDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Firstima.h"
#include "FirstimaDlg.h"
#include "afxdialogex.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


const UINT TimerFirst = 100;
const UINT TimerSecond = 200;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CFirstimaDlg 对话框



CFirstimaDlg::CFirstimaDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CFirstimaDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFirstimaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CFirstimaDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()//定时器
	ON_BN_CLICKED(IDCANCEL, &CFirstimaDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


// CFirstimaDlg 消息处理程序

BOOL CFirstimaDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	//禁止改变窗体大小   禁用最大化
	ModifyStyle(WS_THICKFRAME | WS_MAXIMIZEBOX, 0, SWP_FRAMECHANGED | SWP_DRAWFRAME);
	// TODO:  在此添加额外的初始化代码

	//注册定时器
	//SetTimer(TimerFirst, 200, NULL);//设置定时器1  用于截图
	SetTimer(TimerSecond, 100, NULL);//设置定时器1  用于加载视频

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CFirstimaDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CFirstimaDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CFirstimaDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CFirstimaDlg::OnBnClickedCancel()
{
	// 退出前弹窗提示
	if (MessageBox(L"您确定要退出程序吗？", L"提示", MB_ICONSTOP | MB_YESNO) == IDNO)
	{
		KillTimer(TimerFirst); //退出时杀掉定时器1
		return;
	}
		
	CDialogEx::OnCancel();
}


//************************************
// Method:    OnTimer
// FullName:  CFirstimaDlg::OnTimer
// Access:    protected 
// Returns:   void
// Qualifier: TimerFirst用于截屏  TimerSecond用于延迟播放视频
// Parameter: UINT nIDEvent
//************************************
void CFirstimaDlg::OnTimer(UINT nIDEvent)
{
	if (nIDEvent == TimerFirst)
	{
		CreateVideoCapture();
	}
	
	if (nIDEvent == TimerSecond)
	{
		KillTimer(nIDEvent); //初始化结束以后杀掉定时器
		InitMediaPlayer();  //初始化视频播放
	}
}

//************************************
// Method:    InitMediaPlayer
// FullName:  CFirstimaDlg::InitMediaPlayer
// Access:    public 
// Returns:   void
// Qualifier: 播放视频
//************************************
void CFirstimaDlg::InitMediaPlayer()
{
	//CStatic *pStatic = (CStatic*)GetDlgItem(IDC_STATIC);//获取主播放器
	//HWND h = pStatic->GetSafeHwnd();

	//开始播放视频
	
		CBitmap m_bmp;
		if (m_bmp.m_hObject != NULL)//判断 如果已经有值  删除之 ----局部变量不太可能有O(∩_∩)O哈哈~
			m_bmp.DeleteObject();
		
		LPCWSTR Path = L"D://1.bmp";//转成宽字符

		HBITMAP hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
			Path, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
	
		if (hbmp == NULL)//加载失败  返回
			return ;

		m_bmp.Attach(hbmp);
		DIBSECTION ds;
		BITMAPINFOHEADER &bminfo = ds.dsBmih;
		m_bmp.GetObject(sizeof(ds), &ds);
		int cx = bminfo.biWidth; //得到图像宽度
		int cy = bminfo.biHeight; //得到图像高度


		//该段 应该改为将位图缩小至控件大小
		CRect rect;
		GetDlgItem(IDC_STATIC_FIRST)->GetWindowRect(&rect);  //IDC_STATIC_FIRST为控件ID
		ScreenToClient(&rect);
		GetDlgItem(IDC_STATIC_FIRST)->MoveWindow(rect.left, rect.top, cx, cy, true);//调整大小


		CPaintDC dc(GetDlgItem(IDC_STATIC_FIRST));

		CRect rcclient;
		GetDlgItem(IDC_STATIC_FIRST)->GetClientRect(&rcclient);//获取控件矩阵对象
		CDC memdc; //创建设备上下文对象
		memdc.CreateCompatibleDC(&dc);
		CBitmap bitmap;//创建位图对象
		bitmap.CreateCompatibleBitmap(&dc, rcclient.Width(), rcclient.Height());
		memdc.SelectObject(&bitmap);

		//画刷
		CBrush brush;
		brush.CreatePatternBrush(&m_bmp);
		dc.FillRect(rcclient, &brush);
		dc.BitBlt(rcclient.left, rcclient.top, rcclient.Width(), rcclient.Height(),
			&memdc, rcclient.left, rcclient.top, SRCPAINT);
		brush.DeleteObject();

		//if (MessageBox(L"开始播放", L"开始播放", MB_YESNO) == IDNO)
		//{
		//CString m_cstrFileName("E:\\text.avi");
		//AVIFileInit();
		//PAVIFILE avi;
		//int res = AVIFileOpen(&avi, m_cstrFileName, OF_READ, NULL);
		//int n = GetLastError();
		//if (res != AVIERR_OK)
		//{
		//	if (avi != NULL)
		//	{
		//		AVIFileRelease(avi);
		//		return;
		//	}
		//}
		//AVIFILEINFO avi_info;
		//AVIFileInfo(avi, &avi_info, sizeof(AVIFILEINFO));
		//PAVISTREAM pStream;
		//res = AVIFileGetStream(avi, &pStream, streamtypeVIDEO /*video stream*/, 0 /*first stream*/);
		//if (res != AVIERR_OK)
		//{
		//	if (pStream != NULL)
		//	{

		//		AVIStreamRelease(pStream);
		//		AVIFileExit();
		//		return;
		//	}
		//}
		////
		//int iNumFrames;
		//int iFirstFrame;
		//iFirstFrame = AVIStreamStart(pStream);
		//if (iFirstFrame == -1)
		//{
		//	//Error getteing the frame inside the stream
		//	if (pStream != NULL)
		//	{

		//		AVIStreamRelease(pStream);
		//		AVIFileExit();
		//		return;
		//	}
		//}
		//iNumFrames = AVIStreamLength(pStream);
		//if (iNumFrames == -1)
		//{
		//	//Error getteing the number of frames inside the stream
		//	if (pStream != NULL)
		//	{
		//		AVIStreamRelease(pStream);
		//		AVIFileExit();
		//		return;
		//	}
		//}
		////
		//PGETFRAME pFrame;
		//pFrame = AVIStreamGetFrameOpen(pStream, NULL);

		//AVISTREAMINFO streaminfo;
		//AVIStreamInfo(pStream, &streaminfo, sizeof(AVISTREAMINFO));
		//BITMAPINFOHEADER bih2;
		//long lsize = sizeof(bih2);
		//int index = 0;
		//BYTE* pDIB = (BYTE*)AVIStreamGetFrame(pFrame, index); //
		//AVIStreamReadFormat(pStream, index, &bih2, &lsize);
		////

		//BYTE* Bits = new BYTE[bih2.biSizeImage];

		//int width = avi_info.dwWidth;
		//int height = avi_info.dwHeight;

		//img.m_lpBih = (BITMAPINFOHEADER *)new char[sizeof(BITMAPINFOHEADER)+256 * sizeof(RGBQUAD)];
		//LPBITMAPINFO m_pBMI = (BITMAPINFO *)img.m_lpBih;
		//m_pBMI->bmiHeader.biBitCount = 8;
		//m_pBMI->bmiHeader.biClrImportant = 0;
		//m_pBMI->bmiHeader.biClrUsed = 0;
		//m_pBMI->bmiHeader.biCompression = 0;
		//m_pBMI->bmiHeader.biHeight = height;
		//m_pBMI->bmiHeader.biWidth = width;
		//m_pBMI->bmiHeader.biPlanes = 1;
		//m_pBMI->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		//m_pBMI->bmiHeader.biXPelsPerMeter = 0;
		//m_pBMI->bmiHeader.biYPelsPerMeter = 0;
		//m_pBMI->bmiHeader.biSizeImage = height*width;
		//for (int i = 0; i < 256; i++)
		//{
		//	m_pBMI->bmiColors[i].rgbBlue = i;
		//	m_pBMI->bmiColors[i].rgbGreen = i;
		//	m_pBMI->bmiColors[i].rgbRed = i;
		//	m_pBMI->bmiColors[i].rgbReserved = 0;
		//}

		//img.m_lpImage = new BYTE[width*height];
		//for (index = 0; index < iNumFrames; index++)
		//{
		//	AVIStreamRead(pStream, index, 1, Bits, bih2.biSizeImage, NULL, NULL);
		//	for (int i = 0; i < width*height * 4; i += 4)
		//	{
		//		img.m_lpImage[i / 4] = Bits[i];
		//	}
		//	Sleep(20);
		//	Invalidate(FALSE);
		//	MSG msg;
		//	PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
		//	DispatchMessage(&msg);
		//}
	//}
}

//************************************
// Method:    CreateVideoCapture
// FullName:  CFirstimaDlg::CreateVideoCapture
// Access:    public 
// Returns:   void
// Qualifier: 创建截屏
//************************************
void CFirstimaDlg::CreateVideoCapture()
{
	MessageBox(L"开始截屏", L"开始截屏", MB_YESNO);
}
